package com.example.word.ui.activity;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;


import com.example.dict.R;
import com.example.word.bean.User;
import com.example.word.util.HttpUtils;
import com.example.word.util.MessageUtils;
import com.example.word.util.Response;
import com.example.word.widget.ActionBar;

import org.litepal.crud.DataSupport;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

/**
 * 注册
 */
public class RegisterActivity extends Activity {
    private Activity activity;
    private ActionBar mTitleBar;//标题栏
    private EditText etAccount;//手机号
    private EditText etNickName;//昵称
    private EditText etAge;//
    private EditText etPassword;//密码
    private EditText etPasswordSure;//确认密码
    private Button btnRegister;//注册按钮
    private SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activity = this;
        setContentView(R.layout.activity_register);//加载页面
        etAccount = (EditText) findViewById(R.id.et_account);//获取手机号
        etNickName = (EditText) findViewById(R.id.et_nickName);//获取昵称
        etAge = (EditText) findViewById(R.id.et_age);//获取年龄
        etPassword = (EditText) findViewById(R.id.et_password);//获取密码
        etPasswordSure = (EditText) findViewById(R.id.et_password_sure);//获取确认密码
        btnRegister = (Button) findViewById(R.id.btn_register);//获取注册按钮
        mTitleBar = (ActionBar) findViewById(R.id.myActionBar);
        mTitleBar.setData(activity, "注册", R.drawable.ic_back, 0, 0, getResources().getColor(R.color.colorPrimary), new ActionBar.ActionBarClickListener() {
            @Override
            public void onLeftClick() {
                finish();
            }

            @Override
            public void onRightClick() {
            }
        });
        // 设置主题
        if (!com.example.word.ui.activity.MainActivity.colorChange) {
            mTitleBar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            btnRegister.setBackground(getResources().getDrawable(R.drawable.btn_border_fill));
        } else {
            mTitleBar.setBackgroundColor(getResources().getColor(R.color.myColor));
            btnRegister.setBackground(getResources().getDrawable(R.drawable.btn_border_fill_my_color));
        }
        //设置注册点击按钮
        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String account = etAccount.getText().toString();
                String nickName = etNickName.getText().toString();
                String phoneNumber = etAge.getText().toString();
                String password = etPassword.getText().toString();
                String passwordSure = etPasswordSure.getText().toString();
                if ("".equals(account)) {//账号不能为空
                    MessageUtils.show("账号不能为空", activity);
                    return;
                }
                if ("".equals(nickName)) {//昵称不能为空
                    MessageUtils.show("昵称不能为空", activity);
                    return;
                }
                if (phoneNumber.length() != 11) {//电话
                    MessageUtils.show("请输入正确的手机号码", activity);
                    return;
                }
                if (password.length() < 6) {//密码
                    MessageUtils.show("密码长度不能小于6位", activity);
                    return;
                }
                if (!password.equals(passwordSure)) {//密码不一致
                    MessageUtils.show("两次输入的密码不一致", activity);
                    return;
                }
                ProgressDialog dialog3 = new ProgressDialog(activity);
                dialog3.setMessage("正在上传数据···");
                dialog3.show();
                new Handler().postDelayed(new Runnable() {
                    public void run() {
                        User user = DataSupport.where("account = ?", account).findFirst(User.class);
                        if (user == null) {
                            user = new User(account, password, nickName, phoneNumber);
                            user.save();//保存用户信息
                            Intent intent = new Intent(activity, LoginActivity.class);
                            startActivity(intent);
                            MessageUtils.show("注册成功！", activity);
                            finish();
                        } else {
                            MessageUtils.show("该账号已存在，注册失败！", activity);
                        }
                        dialog3.dismiss();
                    }
//                    @Override
//                    public void run() {
//                        Map<String, String> requestParam = new HashMap<>();
//                        requestParam.put("account", account);
//                        requestParam.put("password", password);
//                        requestParam.put("nickName", nickName);
//                        requestParam.put("phoneNumber", phoneNumber);
//
//                        HttpUtils.sendGetRequest("/userRegister", requestParam, Object.class, new HttpUtils.ApiCallback<Object>() {
//                            @Override
//                            public void onSuccess(Response response) {
//                                // 处理请求成功的情况
//                                MessageUtils.show(response.getMsg(), activity);
//                                Intent intent = new Intent(activity, LoginActivity.class);
//                                startActivity(intent);
//                                finish();
//                                dialog3.dismiss();
//                            }
//
//                            @Override
//                            public void onFailure(Response response) {
//                                // 处理请求失败的情况
//                                MessageUtils.show(response.getMsg(), activity);
//                                dialog3.dismiss();
//                            }
//                        });
//                    }
                }, 505);
            }
        });
    }
}
